define(function (require, exports, module) {

    require("apps/grid");
    require("apps/easyui");

    exports.init = function (ctx, uuid) {
        var page = new $.PageDomFactory(uuid);
        var contextWindow = new $.PageWindow();
        var pageComponentList = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "nsdb",
                colbar: "template"
            },
            configs: {
                gridTitle: "日志列表",
                gridUrl: ctx + '/app/api/sysLog/query',
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'temp1',
                        hidden: true
                    },
                    {
                        field: 'userName',
                        title: '人员',
                        width: 150,
                        align: 'left',
                        formatter: function (value, rowData, rowIndex) {
                            var str = "";
                            str += value ? value : "NVL";
                            str += "(";
                            str += rowData.ipAddress ? rowData.ipAddress : "NVL";
                            str += ")";
                            return str;
                        }
                    },
                    {
                        field: 'createDate',
                        title: '时间',
                        width: 150,
                        align: 'left',
                        formatter: function (value, rowData, rowIndex) {
                            return value ? new Date(value).format("yyyy-MM-dd hh:mm:ss") : "<a>NA</a>"
                        }
                    },
                    {
                        field: 'msg',
                        title: '操作信息',
                        width: 300,
                        align: 'left'
                    },
                    {
                        field: 'recorder',
                        title: '日志记录器',
                        width: 150,
                        align: 'left'
                    },
                    {
                        field: 'level',
                        title: '日志级别',
                        width: 150,
                        align: 'left',
                        formatter: function (text, rowData, rowIndex) {
                            if (text == "ERROR") {
                                return "错误日志";
                            } else if (text == "DEBUG") {
                                return "测试日志";
                            } else if (text == "INFO") {
                                return "操作日志";
                            } else if (text == "TRACE") {
                                return "跟踪日志";
                            } else if (text == "WARN") {
                                return "警告日志";
                            } else {
                                return text;
                            }
                        }
                    }
                ]
            },
            events: {
                renderColBarBefore: function () {
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "view":
                            whenView(context, row);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "del":
                            context.del(btn.href);
                            break;
                        case "package":
                            whenPackage(context, btn.href);
                            break;
                        case "report":
                            whenGotoReport(context, btn.href);
                            break;
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            context.search(btn.href, "日志搜索", "log_search");
                            break;
                        default :
                            break;
                    }
                }
            }
        });


        function whenPackage(context, href) {
            //debugger;
            function package(postData) {
                $("body").wait("start", "日志正在打包，请稍后！");
                $.ajax({
                    type: 'POST',
                    dataType: "json",
                    url: href,
                    data: postData,
                    success: function (data) {
                        if ($.utils.isError(data)) {
                            $.messager.alert('信息',
                                data.msg, 'error');
                            $("body").wait("end");
                        } else {
                            $.messager.alert('信息',
                                '打包成功', 'info');
                        }
                        context.grid.datagrid('uncheckAll');
                        $("body").wait("end");
                    }
                });
            }

            var firstSelected = context.grid.datagrid('getChecked');
            if (firstSelected.length == 0) {
                $.messager.confirm("提示", "您未选择需要打包的日志，系统将对所有日志进行打包，是否继续进行？", function (arg) {
                    if (arg) {
                        package($.utils.buildJson({all: true}));
                    }
                });
                return;
            }
            $.messager.confirm("提示", "确认要打包日志？", function (f) {
                if (f) {
                    var ids = firstSelected.map(function (item) {
                        return item.id;
                    });
                    package($.utils.buildJson({ids: ids}));
                }
            });
        }

        function whenGotoReport(context, href) {
            contextWindow.create({
                href: href,
                title: "图形报表",
                width: 1000
            });
            contextWindow.open();
        }

        function whenView(context, row) {
            context.detail("日志查看", row);
        }
    };

    exports.search = function(ctx, uuid){
        var page = new $.PageDomFactory(uuid, true);
        window.log_search = function(win) {
            win.wait("start", "搜索中,请稍候.");
            var form = page.getJqDom("form-org-search-info");
            var postData = form.serializeJson();
            win.callback(postData);
            win.close();
        }
    }
});
